/*
 * Order.java
 *
 * Created on 27 March 2007, 11:49
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package usyd.comp5348;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Entity class Order
 *
 * @author Yuezhong Zhang
 */
@Entity(name="Orders")
public class Order implements Serializable {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO, generator="OrdIdSeq")
    @SequenceGenerator(name="OrdIdSeq", sequenceName="ord_id_seq")
    private Integer id;
        
    @OneToMany(mappedBy="order",cascade={CascadeType.ALL})
    private List<OrderLine> orderLines;
    
    public List<OrderLine> getOrderLines() {
        return orderLines;
    }
    
    public Integer getId() {
        return this.id;
    }

    public void setOrderLines(List<OrderLine> orderLines) {
        this.orderLines = orderLines;
    }
    
    
    /** Creates a new instance of Order */
    public Order() {
    }
    
    public Order(OrderInfo o) {
        setCustomerId(o.getCustomerId());
        setDate(o.getOrderDate());
        if (o.getId() != OrderInfo.UNDEFINED)
            setId(o.getId());
        List<OrderLine> ol = new ArrayList<OrderLine>();
        for (OrderLineInfo line : o.getVideos()) {
            ol.add(new OrderLine(line, this));
        }
        setOrderLines(ol);
        setStatus(o.getStatus());
        setStore(o.getStore());
        setTotal(o.getTotal());
        setWarehouse(o.getWarehouse());
    }
    
    public OrderInfo getOrderInfo() {
        OrderInfo ret = new OrderInfo(getId(), getCustomerId(), getTotal(), null, getDate(), getWarehouse(), getStore(), getStatus());
        OrderLineInfo[] lines = new OrderLineInfo[this.getOrderLines().size()];
        int i=0;
        for (OrderLine line : this.getOrderLines()) {
            lines[i] = line.getOrderLineInfo(ret);
            ++i;
        }
        ret.setVideos(lines);
        return ret;
    }
    
    /**
     * Sets the id of this Order to the specified value.
     * @param id the new id
     */
    public void setId(Integer id) {
        this.id = id;
    }
    
    /**
     * Returns a hash code value for the object.  This implementation computes
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
    
    /**
     * Determines whether another object is equal to this Order.  The result is
     * <code>true</code> if and only if the argument is not null and is a Order object that
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Order)) {
            return false;
        }
        Order other = (Order)object;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
        return true;
    }
    
    /**
     * Returns a string representation of the object.  This implementation constructs
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return "usyd.comp5348.entity.Order[id=" + id + "]";
    }
    
    /**
     * Holds value of property customerId.
     */
    @Column(name="customer_id", nullable = false)
    private Integer customerId;
    
    /**
     * Getter for property customerId.
     * @return Value of property customerId.
     */
    public Integer getCustomerId() {
        return this.customerId;
    }
    
    /**
     * Setter for property customerId.
     * @param customerId New value of property customerId.
     */
    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }
    
    /**
     * Holds value of property total.
     */
    @Column(name="total", nullable =false)
    private float total;
    
    /**
     * Getter for property total.
     * @return Value of property total.
     */
    public float getTotal() {
        return this.total;
    }
    
    /**
     * Setter for property total.
     * @param total New value of property total.
     */
    public void setTotal(float total) {
        this.total = total;
    }
    
    /**
     * Holds value of property date.
     */
    @Column(name="order_date",nullable=false)
    @Temporal(value = TemporalType.DATE)
    private Date date;
    
    /**
     * Getter for property orderDate.
     * @return Value of property orderDate.
     */
    public Date getDate() {
        return this.date;
    }
    
    /**
     * Setter for property orderDate.
     * @param orderDate New value of property orderDate.
     */
    public void setDate(Date date) {
        this.date = date;
    }
    
    /**
     * Holds value of property warehouse.
     */
    @Column(name="warehouse", length=16,nullable=false)
    private String warehouse;
    
    /**
     * Getter for property warehouse.
     * @return Value of property warehouse.
     */
    public String getWarehouse() {
        return this.warehouse;
    }
    
    /**
     * Setter for property warehouse.
     * @param warehouse New value of property warehouse.
     */
    public void setWarehouse(String warehouse) {
        this.warehouse = warehouse;
    }
    
    /**
     * Holds value of property store.
     */
    @Column(name="store",length=16,nullable=false)
    private String store;
    
    /**
     * Getter for property store.
     * @return Value of property store.
     */
    public String getStore() {
        return this.store;
    }
    
    /**
     * Setter for property store.
     * @param store New value of property store.
     */
    public void setStore(String store) {
        this.store = store;
    }
    
    /**
     * Holds value of property status.
     */
    @Column(name="status",nullable = false)
    private int status;
    
    /**
     * Getter for property status.
     * @return Value of property status.
     */
    public int getStatus() {
        return this.status;
    }
    
    /**
     * Setter for property status.
     * @param status New value of property status.
     */
    public void setStatus(int status) {
        this.status = status;
    }
    
}
